class_name EnemyBornHole
extends Marker2D

@export var enemies_weight: Dictionary[String, float]
@export var born_time : float  = 3.0

@onready var time_node = $Timer

var total_weight: float = 0.0

func _ready() -> void:
	$Polygon2D.queue_free()
	for w in enemies_weight.values():
		total_weight += w

func wake_up():
	time_node.start(born_time)
	_on_timer_timeout()

func _on_timer_timeout() -> void:
	born_enemy()

func _get_enemy_type_by_weight(weight: float) -> String:
	var temp_sum = 0
	for type in enemies_weight.keys():
		temp_sum += enemies_weight[type]
		if temp_sum >= weight:
			return type
	print("weight over total weight")
	return ""

func born_enemy():
	if enemies_weight.size() == 0:
		return
	
	var rand_weight := randf_range(0.0, total_weight)
	var select_enemy_type := _get_enemy_type_by_weight(rand_weight)
	
	if select_enemy_type == "":
		return
	
	var enemy_node = EnemyManager.get_enemy(select_enemy_type)
	add_child(enemy_node)
	enemy_node.global_position = global_position
